package com.baizhi.user.controller;


import com.baizhi.user.entity.Admin;
import com.baizhi.user.entity.ResultEntity;
import com.baizhi.user.service.IAdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhenggang
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/user")
public class AdminController {
    @Autowired
    private IAdminService as;
    @GetMapping("/login")
    public ResultEntity<Admin> login(Admin admin){
        QueryWrapper<Admin> wrapper=new QueryWrapper<>();
        wrapper.eq("username",admin.getUsername()).eq("password",admin.getPassword());
        Admin one = as.getOne(wrapper);
        if(one!=null){
            return ResultEntity.ok(one);
        }else{
            return ResultEntity.error("error");
        }
    }

    @GetMapping("/showAll")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public ResultEntity showAll(){
        List<Admin> list = as.list();
        return ResultEntity.ok(list);
    }
    @PostMapping("/insert")
    public ResultEntity insert(@RequestBody Admin admin){
        as.save(admin);
        return ResultEntity.ok();
    }
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:user:add2')")
    public ResultEntity delete(@PathVariable("id") int id){
        as.removeById(id);
        return ResultEntity.ok();
    }
}

